package com.bs.service.util;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.*;

/**
 * Created by wenguangjia on 2017/6/1.
 */
public class FtpUtil {
    public static boolean upload(String ip, int port, String username, String password, String serverPath, String localpath, String fileName,String localFileName) {
        Session session = null;
        Channel channel = null;
        try {
            JSch jsch = new JSch();
            if (port <= 0) {
                //连接服务器，采用默认端口
                session = jsch.getSession(username, ip);
            } else {
                //采用指定的端口连接服务器
                session = jsch.getSession(username, ip, port);
            }
            //如果服务器连接不上，则抛出异常
            if (session == null) {
                throw new Exception("session is null");
            }

            //设置登陆主机的密码
            session.setPassword(password);//设置密码
            //设置第一次登陆的时候提示，可选值：(ask | yes | no)
            session.setConfig("StrictHostKeyChecking", "no");
            //设置登陆超时时间
            session.connect(30000);

            //创建sftp通信通道
            channel = (Channel) session.openChannel("sftp");
            channel.connect(1000);
            ChannelSftp sftp = (ChannelSftp) channel;

            //进入服务器指定的文件夹
            sftp.cd(serverPath);


            //以下代码实现从本地上传一个文件到服务器，如果要实现下载，对换以下流就可以了
            OutputStream outstream = sftp.put(fileName);
            InputStream instream = new FileInputStream(new File(localpath+localFileName));

            byte b[] = new byte[1024];
            int n;
            while ((n = instream.read(b)) != -1) {
                outstream.write(b, 0, n);
            }

            outstream.flush();
            outstream.close();
            instream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            session.disconnect();
            channel.disconnect();
        }
    }

    public static boolean download(String ip, int port, String username, String password, String serverPath, String localpath, String fileName,String localFileName) {
        Session session = null;
        Channel channel = null;
        try {
            JSch jsch = new JSch();
            if (port <= 0) {
                //连接服务器，采用默认端口
                session = jsch.getSession(username, ip);
            } else {
                //采用指定的端口连接服务器
                session = jsch.getSession(username, ip, port);
            }
            //如果服务器连接不上，则抛出异常
            if (session == null) {
                throw new Exception("session is null");
            }

            //设置登陆主机的密码
            session.setPassword(password);//设置密码
            //设置第一次登陆的时候提示，可选值：(ask | yes | no)
            session.setConfig("StrictHostKeyChecking", "no");
            //设置登陆超时时间
            session.connect(30000);


            //创建sftp通信通道
            channel = (Channel) session.openChannel("sftp");
            channel.connect(1000);
            ChannelSftp sftp = (ChannelSftp) channel;


            //进入服务器指定的文件夹
            sftp.cd(serverPath);

            //列出服务器指定的文件列表
//            Vector v = sftp.ls("*");
//            for (int i = 0; i < v.size(); i++) {
//                System.out.println(v.get(i));
//            }

            //以下代码实现从本地下载一个文件到服务器，如果要实现下载，对换以下流就可以了
            InputStream instream = sftp.get(fileName);
            FileOutputStream outstream=new FileOutputStream(new File(localpath+localFileName));
            byte b[] = new byte[1024];
            int n;
            while ((n = instream.read(b)) != -1) {
                outstream.write(b, 0, n);
            }

            outstream.flush();
            outstream.close();
            instream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            session.disconnect();
            channel.disconnect();
        }
    }
}
